<?php
namespace App\Http\Controllers\Home;

use App\Models\Agent;
use App\Models\Agentlev;
use Image;

Class AgentController extends BaseController
{

    public function __construct(Agent $agent, Agentlev $agentlev)
    {
        parent::__construct();
        $this->agent = $agent;
        $this->agentlev = $agentlev;
    }

    public function getBank()
    {
        $data = $this->agent->getOne(['id' => session('agent')['aid']]);
        return view("home.bank", compact('data'));
    }

    public function postBank()
    {
        $res = $this->agent->editBank(request());
        return redirect()->back()->with('message', $res);
    }

    public function getAgentInfo()
    {
        $file = public_path().'/upload/agent/'.session('agent')['aid'].'.png';
//        if (!file_exists($file)) {
//            $this->generateBook();
//        }
        return view("home.agent_info");
    }

    public function getGenerateBook()
    {
        $file = $this->generateBook();
        if (file_exists($file)) {
           echo 1;exit;
        }
        echo 0;exit;
    }

    public function generateBook()
    {
        $file = public_path().'/upload/agent/'.session('agent')['aid'].'.png';
        if (file_exists($file)) {
            unlink($file);
        }
        $agentInfo = $this->agent->getOne(['id'=>session('agent')['aid']]);
        if (!$agentInfo['accredit_no']) {
            $data['accredit_no'] = time();
            $data['accredit_start_time'] = time();
            $data['accredit_end_time'] = strtotime("+1 year");
            $this->agent->up(['id'=>session('agent')['aid']],$data);

            $agentInfo['accredit_no'] = $data['accredit_no'];
            $agentInfo['accredit_start_time'] = $data['accredit_start_time'];
            $agentInfo['accredit_end_time'] = $data['accredit_end_time'];
        }
        $agent = $this->agent->getAgentLevRateById(session('agent')['aid'],session('agent')['bid']);
        $imgfile = public_path().'/upload/brand/'.session('agent')['bid'].'.png';
        if (!file_exists($imgfile)) {
            return '';
        }
        // 获取图片源
        $img = Image::make($imgfile);
        $img->text(session('agent')['name'], 197, 414, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(25);            //设置文字大小
        });
        $img->text($agent['lev_name'], 248, 570, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(30);            //设置文字大小
        });
        $img->text(session('agent')['wx'], 261, 640, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(18);            //设置文字大小
        });
        $timestr = date('Y/m/d',$agentInfo['accredit_start_time']).' - '.date("Y/m/d",$agentInfo['accredit_end_time']);
        $img->text($timestr, 261, 666, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(15);            //设置文字大小
        });
        $img->text($agentInfo['accredit_no'], 261, 700, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(18);            //设置文字大小
        });
        $img->text(date('Y',time()), 277, 950, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(15);            //设置文字大小
        });
        $img->text(date('m',time()), 333, 950, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(15);            //设置文字大小
        });
        $img->text(date('d',time()), 365, 950, function ($font) {
            $font->file(public_path().'/font/hykt.ttf'); //设置字体
            $font->size(15);            //设置文字大小
        });
        // 将处理后的图片重新保存到其他路径
        $img->save(public_path().'/upload/agent/'.session('agent')['aid'].'.png');
        return $file;
    }

    public function getShare()
    {
        $aid = session('agent');
        $data = $this->agent->getAgentInfo($aid['aid']);
        $bid = $data['data']['bid'];
        $lev = $data['data']['lev'];
        if (strstr($data['data']['bid'],',')) {
            $bid = explode(',',$data['data']['bid'])[1];
        }
        if (strstr($data['data']['lev'],',')) {
            $lev = explode(',',$data['data']['lev'])[1];
        }
        $where['bid'] = $bid;
        $where['lev'] = ['>=',$lev];
        $levs = $this->agentlev->getList($where)->toArray();
        $data['data']['lev_str'] = '';
        if ($levs) {
            foreach ($levs as $v) {
                if ($lev == $v['lev']) {
                    $data['data']['lev_str'] = $v['lev_name'];
                }
            }
        }
        $agent = $data['data'];
        return view("home.share",compact('agent','levs'));
    }

    public function getInviteAgentUrl()
    {
        $aid = session('agent');
        $lev = $_GET['lev'];
        $lev_info = $this->agentlev->getAgentLev(array('id' => $lev));
        $url = 'invite/register?upid=' . $aid['aid'] . '&lev=' . $lev;
        return view('home.inviteto')->with('url', $url)->with('lev', $lev_info);
    }

    public function getAgentView()
    {
        return view("home.viewagent");
    }

    public function getCheckAgent()
    {
        return view('home.check_agent');
    }

    public function postCheckAgent()
    {
        $res = $this->agent->checkAgent(request());
        return redirect()->back()->with('message', $res)->withInput();
    }

    public function getFxList()
    {
        $aid = session('agent');
        $data = $this->agent->getAgentInfo($aid['aid']);
        $bid = $data['data']['bid'];
        $lev = $data['data']['lev'];
        if (strstr($data['data']['bid'],',')) {
            $bid = explode(',',$data['data']['bid'])[1];
        }
        if (strstr($data['data']['lev'],',')) {
            $lev = explode(',',$data['data']['lev'])[1];
        }
        $where['bid'] = $bid;
        $where['lev'] = ['>=',$lev];
        $levs = $this->agentlev->getList($where)->toArray();
        $data['data']['lev_str'] = '';
        if ($levs) {
            foreach ($levs as $v) {
                if ($lev == $v['lev']) {
                    $data['data']['lev_str'] = $v['lev_name'];
                }
            }
        }
        $agent = $data['data'];
        return view("home.agent_fenxiao",compact('agent','levs'));
    }

    public function getFxDetail()
    {
        $lev = $this->agentlev->getOne(array('id' => $_GET['lev']));
        $rows = $this->agent->getFxData(array('lev' => $lev['lev'], 'pid' => session('agent')['aid']));
        return view('home.agent_fenxiao_detail', compact('rows', 'lev'));
    }

}
